import request from '@/utils/request'
import type { Role, RoleListParams, AssignPermissionsParams } from '@/types/role'
import type { PageResult } from '@/types/common'

/**
 * 获取角色列表
 */
export function getRoleList(params: RoleListParams) {
  return request<PageResult<Role>>({
    url: '/tenant/role/list',
    method: 'get',
    params,
  })
}

/**
 * 获取角色的权限列表（返回权限代码）
 */
export function getRolePermissions(id: number) {
  return request<string[]>({
    url: `/tenant/role/${id}/permissions`,
    method: 'get',
  })
}

/**
 * 创建角色
 */
export function createRole(data: Partial<Role>) {
  return request<Role>({
    url: '/tenant/role',
    method: 'post',
    data,
  })
}

/**
 * 更新角色
 */
export function updateRole(id: number, data: Partial<Role>) {
  return request<void>({
    url: `/tenant/role/${id}`,
    method: 'put',
    data,
  })
}

/**
 * 删除角色
 */
export function deleteRole(id: number) {
  return request<void>({
    url: `/tenant/role/${id}`,
    method: 'delete',
  })
}

/**
 * 分配权限给角色
 */
export function assignPermissions(id: number, data: AssignPermissionsParams) {
  return request<void>({
    url: `/tenant/role/${id}/permissions`,
    method: 'put',
    data,
  })
}

